<?php

declare(strict_types=1);

namespace App\Controller;
use App\Model\data\CurrentShopData;
use App\Model\data\CurrentUserData;
use App\Service\NotifyLog\ExpressLogService;
use Hyperf\Di\Annotation\Inject;


/**
 * 物流通知消息
 */
class ExpressLogController extends AbstractController
{
    /**
     * 物流等信息
     * @Inject
     * @var ExpressLogService
     */
    private  $expressLogService;


    public function shop_list()
    {
        $is_read = $this->request->input('is_read','');
        $up_time = $this->request->input('up_time','0');
        $shop_info  = CurrentShopData::getData();
        $list =  $this->paginate($this->expressLogService->list(intval($shop_info->id),$is_read,2,$up_time))->toArray();
        if($list['data']!=[]){
            foreach ($list['data'] as $key=>$value){
                $total =  array_sum(array_column($value['order_info']['with_products'],'quantity'));
                $string = implode(',',array_column($value['order_info']['with_products'],'product_name'));
                $string  = mb_substr($string,0,50,'utf-8');
                $list['data'][$key]['content'] = '您购买的['.$string.'...]等,共'.$total.'件商品,'.ExpressLogService::ORDER_STATUS[$value['order_status']];
            }
        }
        $list['un_read'] = $this->expressLogService->count_size(intval($shop_info->id),2);
//        物流更新信息
        return $this->jsonResponse->success($list,'信息列表');
    }

    public function shop_count()
    {
//        物流信息未读数据
        $shop_info  = CurrentShopData::getData();
        return $this->jsonResponse->success($this->expressLogService->count_size(intval($shop_info->id),2),'信息列表');
    }

    public function user_list()
    {
        $is_read = $this->request->input('is_read','');
        $up_time = $this->request->input('up_time',0);
        $user_info  = CurrentUserData::getData();
        $list =  $this->paginate($this->expressLogService->list(intval($user_info->id),$is_read,1,$up_time))->toArray();
        if($list['data']!=[]){
            foreach ($list['data'] as $key=>$value){
                $total =  array_sum(array_column($value['order_info']['with_products'],'quantity'));
                $string = implode(',',array_column($value['order_info']['with_products'],'product_name'));
                $string  = mb_substr($string,0,20,'utf-8');
                $list['data'][$key]['content'] = '您购买的['.$string.'...]等,共'.$total.'件商品,'.ExpressLogService::ORDER_STATUS[$value['order_status']];
            }
        }
        $list['un_read'] = $this->expressLogService->count_size(intval($user_info->id),1);
        return $this->jsonResponse->success($list,'信息列表');
    }

    public function user_count()
    {
        $user_info  = CurrentUserData::getData();
        return $this->jsonResponse->success($this->expressLogService->count_size(intval($user_info->id),1),'信息列表');
    }

}